//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.log.service;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.base.domain.UserLog;
import com.ruoyi.live.base.mapper.UserLogMapper;
import com.ruoyi.live.log.CheckMobile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import jakarta.servlet.http.HttpServletRequest;

@Service("com.itmc.LoggerService")
public class LoggerService {
    @Autowired
    UserLogMapper dao;
    @Autowired
    CacheService cacheService;

    public LoggerService() {
    }

    public UserLog loadLogger(HttpServletRequest request) {
        UserLog logger = new UserLog();

        try {
            String userAgent = request.getHeader("USER-AGENT");
            String path = request.getRequestURI();
            String paramData = JSON.toJSONString(request.getParameterMap());
            logger.setLogIp(this.getCliectIp(request)).setLogMethod(request.getMethod()).setLogType(this.getRequestType(request)).setLogParamData(paramData).setLogUri(path).setLogToken(request.getHeader("Authorization")).setLogSource(request.getHeader("Source")).setLogCreateTime(System.currentTimeMillis()).setLogIsPhone(CheckMobile.check(userAgent) ? 1 : 2);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return logger;
    }

    @Async
    public void saveLogger(UserLog logger) {
        if ("1".equals(this.cacheService.getParam("SYSTEM_INSERT_LOG"))) {
            try {
                this.dao.insert(logger);
            } catch (Exception var5) {
                try {
                    logger.setLogParamData("太长不写任性");
                    logger.setLogSource("");
                    this.dao.insert(logger);
                } catch (Exception var4) {
                    var4.printStackTrace();
                }
            }
        }

    }

    private String getCliectIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.trim() == "" || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        String[] arr = ip.split(",");
        String[] var4 = arr;
        int var5 = arr.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String str = var4[var6];
            if (!"unknown".equalsIgnoreCase(str)) {
                ip = str;
                break;
            }
        }

        return ip;
    }

    private String getRequestType(HttpServletRequest request) {
        return request.getHeader("X-Requested-With");
    }
}
